<template>
	<view class="record">
		<view class="header">
			<uni-data-select v-model="value1" :localdata="headerList" @change="change3"
				:clear="false"></uni-data-select>
			<uni-data-select v-model="value3" :localdata="dateList" @change="change1" :clear="false"></uni-data-select>
		</view>
		<view v-if="RecordList.length==0" class="np f-c-c">
			<view class="iconfont icon-queshengtu cb" style="font-size: 90rpx;"></view>
			<view class="f12 cb mt20">暂无明细</view>
		</view>
		<view class="content" v-else>
			<uni-card v-for="item in RecordList" :key="item.id">
				<view class="card-container">
					<view class="left">
						<view class="tradTime">{{item.create_time}}提现申请</view>
						<view class="withTime" v-if="item.with_status == 'SUCCESS'">{{item.with_time}}</view>
						<view class="">门店：{{nickname}}</view>
						<view class="" v-if="item.type == 'alipay'">账户类型：支付宝</view>
						<view class="" v-if="item.type == 'hz'">账户类型：抖音</view>
						<view class="" v-if="item.with_status == 'SUCCESS'">提现单号：{{item.with_no}}</view>
						<view class="" v-else>提现单号：无</view>
						<view class="" v-if="item.with_status == 'SUCCESS'">抖音提现单号:{{item.dy_with_no}}</view>
						<view class="" v-else>抖音提现单号：无</view>
					</view>
					<view class="right" @click="goDetail(item)">
						<view class="top">
							<view class="withPrice" v-if="item.with_status == 'SUCCESS'">￥{{item.balance}}</view>
							<view class="withPrice" v-if="item.with_status == null" style="color: yellowgreen;">
								￥{{item.balance}}</view>
							<view class="withPrice" v-if="item.with_status == 'FAIL'" style="color: red;">
								￥{{item.balance}}</view>
							<uni-icons type="right" color="#bfbcbf" size="16"></uni-icons>
						</view>
						<view class="withStatus" v-if="item.with_status == 'SUCCESS'">
							提现成功
						</view>
						<view class="withStatus"
							v-if="item.with_status == null || item.with_status =='' || item.with_status == 'NOT_FOUND'"
							style="color: yellowgreen;">
							未提现
						</view>
						<view class="withStatus" v-if="item.with_status == 'FAIL'" style="color: red;">提现失败</view>
						<view class="withStatus" v-if="item.with_status == 'PROCESSING'">处理中</view>
						<view class="withStatus" v-if="item.with_status == 'REEXCHANGE'">退票</view>
					</view>
				</view>
			</uni-card>
		</view>
		<!-- 导出 -->
		<view class="export">
			<u-button text="导出记录" type="success" @click="open"></u-button>
		</view>
		<u-calendar :show="calendarShow" mode="range" :showMark="true" @confirm="confirm" :closeOnClickOverlay="true"
			@close="calendarShow = false"  monthNum="13" :minDate="minDate" :maxDate="maxDate" :defaultDate="defaultDate"></u-calendar>
	</view>
</template>

<script>
	import config from '@/custom/config.js';
	export default {
		data() {
			return {
				// 提现记录
				params: {
					page: 1,
					limit: 10,
					with_status: '',
					start_date: '',
					end_date: '',
				},
				nickname: '',
				title1: '全部',
				title2: '近三天',
				headerList: [{
						text: '全部',
						value: 0,
					},
					{
						text: '提现成功',
						value: 1,
					},
					{
						text: '提现失败',
						value: 2,
					}
				],
				nameList: [{
					label: '',
					value: 0,
				}],
				value1: 0,
				value3: 0,
				dateList: [{
						text: '近三天',
						value: 0,
					},
					{
						text: '近七天',
						value: 1,
					},
					{
						text: '近三十天',
						value: 2,
					},
					{
						text: '近三个月',
						value: 3,
					},
					{
						text: '近半年',
						value: 4,
					},
					{
						text: '近一年',
						value: 5,
					}
				],
				value2: 0,
				RecordList: [],
				show: false,
				mode: 'range',
				calendarShow: false,
				minDate: '',
				maxDate: '',
				defaultDate: []
			}
		},
		methods: {
			filterMoney(num) {
				return (num / 100).toFixed(2)
			},
			getRecordList() {
				uni.showLoading()
				this.beg.request({
					url: '/index.php/admin/store.Store/get_with_record',
					method: 'post',
					data: this.params
				}).then(res => {
					// console.log(res)
					uni.hideLoading()
					res.data.data.map(item => {
						item.balance = this.filterMoney(item.balance)
					})
					this.RecordList = res.data.data
				})
			},
			// 跳转详情
			goDetail(item) {
				uni.navigateTo({
					url: "/pages/home/recordDetail/recordDetail?item=" + encodeURIComponent(JSON
						.stringify(item))
				})
			},
			change1(e) {
				// console.log(e)
				this.value1 = e
				if (e == 0) {
					this.getDaysAgo(3)
					this.getRecordList()
				} else if (e == 1) {
					this.getDaysAgo(7)
					this.getRecordList()
				} else if (e == 2) {
					this.getDaysAgo(30)
					this.getRecordList()
				} else if (e == 3) {
					this.getDaysAgo(90)
					this.getRecordList()
				} else if (e == 4) {
					this.getDaysAgo(180)
					this.getRecordList()
				} else if (e == 5) {
					this.getDaysAgo(365)
					this.getRecordList()
				}
			},
			change3(e) {
				// console.log(e);
				if (e == 0) {
					this.params.with_status = ''
					this.params.page = 1
					this.getRecordList()
				} else if (e == 1) {
					this.params.with_status = 'SUCCESS'
					this.params.page = 1
					this.getRecordList()
				} else if (e == 2) {
					this.params.with_status = 'FAIL'
					this.params.page = 1
					this.getRecordList()
				}
			},
			// 获取近三天的开始与结束日期
			getLastThreeDays() {
				const threeDaysAgo = new Date(Date.now() - 3 * 24 * 60 * 60 * 1000);
				const threeDaysAgoFormatted = threeDaysAgo.toISOString().split('T')[0];
				const today = new Date();
				const todayFormatted = today.toISOString().split('T')[0];
				this.params.start_date = `${threeDaysAgoFormatted}`
				this.params.end_date = `${todayFormatted}`
			},
			// 获取时间
			getDaysAgo(days) {
				const date = new Date();
				date.setDate(date.getDate() - days);
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				this.params.start_date = `${year}-${month}-${day}`
				this.params.page = 1
			},
			open() {
				this.calendarShow = true
			},
			chooseTimed() {
				let date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (month <= 9) {
					month = '0' + month;
				}
				if (day <= 9) {
					day = '0' + day;
				}
				let minyear = year - 1;
				this.minDate = minyear + '-' + month + '-' + day;
				let maxyear = year + 1;
				this.maxDate = year + '-' + month + '-' + day;
				this.defaultDate[0] = year + '-' + month + '-' + day;
			},
			// 导出
			confirm(e) {
				uni.showLoading()
				this.calendarShow = false
				var adminInfo = uni.getStorageSync("adminInfo")
				var oem_id = adminInfo.oem_id
				uni.request({
					url: 'https://life.kdsh.cc/index.php/admin/store.Store/get_with_record_export',
					data: {
						page: 1,
						limit: 10,
						start_date: e[0],
						end_date: e[1],
						oem_id: oem_id
					},
					method: "POST",
					header: {
						Accept: "application/json, text/plain, */*",
						contentType: config.contentType,
						agentId: adminInfo.agent_id,
						batoken: adminInfo.token,
						chainStoresId: adminInfo.chain_stores_id,
						oemId: adminInfo.oem_id,
						partnerId: adminInfo.partner_id,
						server: "true",
						storeId: adminInfo.store_id,
						thinkLang: "zh-cn",
					},
					responseType: 'arraybuffer',
					success(res) {
						const now = new Date();
						const fileName = now.toISOString() + '.xlsx';
						const arrayBuffer = res.data; // utf-8编码的文件数据
						const base64String = uni.arrayBufferToBase64(arrayBuffer);
						const buffer = uni.base64ToArrayBuffer(base64String);
						let fs = uni.getFileSystemManager();
						const filePath = wx.env.USER_DATA_PATH + '/' + fileName;
						fs.writeFile({
							filePath: filePath,
							data: buffer,
							encoding: 'binary',
							success: (res) => {
								uni.hideLoading()
								uni.openDocument({
									filePath: filePath,
									showMenu: true,
									success: (res) => {
										console.log('文件预览成功');
										// 构建分享内容
									},
									fail: (error) => {
										console.error('文件保存失败', error);
									}
								})

							},
							fail(err) {
								uni.hideLoading()
							}

						})
					},
					fail: () => {
						uni.hideLoading()
					}
				})
			}
		},
		onReachBottom() {
			this.params.page++
			uni.showLoading()
			this.beg.request({
				url: '/index.php/admin/store.Store/get_with_record',
				method: 'post',
				data: this.params
			}).then(res => {
				uni.hideLoading()
				var info = uni.getStorageSync('userInfo')
				this.username = info.nickname
				res.data.data.map(item => {
					item.balance = this.filterMoney(item.balance)
				})
				this.RecordList = this.RecordList.concat(res.data.data)
			})
		},
		onLoad() {
			this.getLastThreeDays()
			this.getRecordList()
			var userInfo = uni.getStorageSync("adminInfo")
			this.nickname = userInfo.nickname
			this.nameList[0].label = userInfo.nickname
			this.chooseTimed()
		}
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	.record {
		.header {
			width: 100%;
			position: fixed;
			top: 0;
			background-color: #fff;
			z-index: 1000;
			display: flex;
			justify-content: space-around;
			align-items: center;

			::v-deep .uni-select {
				border: 0;
				height: 30px;
				width: 160rpx;
				font-size: 15px;
			}

			::v-deep .uni-select__input-text {
				font-weight: 700;
			}
		}

		.content {
			padding-top: 50rpx;

			.card-container {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.left {
					.tradTime {
						font-size: 30rpx;
						font-weight: 600;
						color: #0f0d0f;
					}

					.tradName {}

					.withTime {}
				}

				.right {
					.top {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.withPrice {
							font-size: 30rpx;
							font-weight: 600;
							color: #535153;
						}
					}

					.withStatus {}
				}
			}
		}

		.export {
			position: fixed;
			// left: 244rpx;
			left: 42%;
			bottom: 100rpx;
		}

		::v-deep .u-calendar__action {
			display: flex;
			align-items: center;

		}
	}
</style>